/**
 * Created by Ynlh on 15/10/21.
 */
var speed = 0.04;

var pos = [68.125, 189.125, 310.125, 431.125];

var numColors = {
    4 : cc.color("#776e65"),
    8 : cc.color("#f9f6f2")
};

var tileColors = {
    2 : cc.color("#eee4da"),
    4 : cc.color("#ede0c8"),
    8 : cc.color("#f2b179"),
    16 : cc.color("#f59563"),
    32 : cc.color("#f67c5f"),
    64 : cc.color("#f65e3b"),
    128 : cc.color("#edcf72"),
    256 : cc.color("#edcc61"),
    512 : cc.color("#edc850"),
    1024 : cc.color("#edc53f"),
    2048 : cc.color("#edc22e"),
    4096 : cc.color("#3c3a32")
};

var Tile = function (position, value) {
    this.sprite = new cc.Scale9Sprite(res.bg_tile);
    this.sprite.attr({
        x : pos[position.x],
        y : pos[position.y],
        width : 106.25,
        height : 106.25,
        color : cc.color("#ccc0b3"),
        cascadeColor : false
    });

    if (value && value > 0) {
        this.x = position.x;
        this.y = position.y;
        this.setValue(value);
        this.runAnim();
    }
};

Tile.prototype.setValue = function (value) {
    this.value = value;
    this.setSize(110, 110);
    this.setColor(value);

    if (this.label == undefined) {
        //this.label = new cc.LabelTTF(value.toString(), "ClearSans-Bold", 60);
        this.label = new cc.LabelBMFont(value.toString(), res.fnt_ClearSans_Bold);
        this.label.setNormalizedPosition(0.5, 0.5);
        this.sprite.addChild(this.label);
    }

    this.label.string = value.toString();
    this.label.color = numColors[value > 4 ? 8 : 4];

    if (value >= 128 && value < 1024)
        this.label.scale = 0.5;
    else if (value >= 1024 && value < 16384)
        this.label.scale = 0.4;
    else if (value >= 16384)
        this.label.scale = 0.35;
    else
        this.label.scale = 0.6;
};

Tile.prototype.runAnim = function () {
    this.sprite.scale = 0;
    var a1 = cc.scaleTo(0.12, 1);
    //var a2 = cc.actionEase(a1);
    //a1.easing(cc.easeInOut(2));
    this.sprite.runAction(a1);
};

Tile.prototype.setSize = function (width, height) {
    this.sprite.width = width;
    this.sprite.height = height;
};

Tile.prototype.setColor = function (value) {
    this.sprite.color = tileColors[value > 4096 ? 4096 : value];
};

Tile.prototype.setPosition = function (position, y) {
    if (y === undefined) {
        this.x = position.x;
        this.y = position.y;
    } else {
        this.x = position;
        this.y = y;
    }

    this.sprite.x = pos[this.x];
    this.sprite.y = pos[this.y];
};

//Tile.prototype.update = function (step) {
//    this.setValue(this.value * 2);
//};
//
//Tile.prototype.move = function (step, isVertical, callback, isRemove) {
//    Game.getInstance().grid.cells[this.x][this.y] = null;
//
//    if (isRemove)
//        this.sprite.removeFromParent();
//    else {
//        if (isVertical)
//            this.y = this.y + step;
//        else
//            this.x = this.x + step;
//        this.setPosition(this.x, this.y);
//    }
//
//    callback(Math.abs(step));
//};

Tile.prototype.update = function (step) {
    var _this = this;
    this.value = this.value * 2;
    //this.sprite.scale = 0;

    var a0 = cc.delayTime(0.05);
    //var a0 = cc.delayTime(Math.abs(step) * speed);
    var a1 = cc.callFunc(function () {
        _this.setValue(_this.value);
    });
    var a2 = cc.scaleTo(0.1, 1.2);
    var a3 = cc.scaleTo(0.1, 1);
    var a4 = cc.actionEase(a3);
    var a5 = cc.sequence(a0, a1, a2, a4);
    this.sprite.runAction(a5);

    Game.getInstance().updateScore(this.value);
};

Tile.prototype.move = function (step, isVertical, callback, isRemove) {
    Game.getInstance().grid.cells[this.x][this.y] = null;

    this.scale = 1;

    var a1 = isVertical
        ? cc.moveBy(0.05, 0, step * 121)
        : cc.moveBy(0.05, step * 121, 0);

    //var a1 = isVertical
    //    ? cc.moveBy(Math.abs(step) * speed, 0, step * 121)
    //    : cc.moveBy(Math.abs(step) * speed, step * 121, 0);

    if (isRemove) {
        var a2 = cc.removeSelf();
        var a3 = cc.sequence(a1, a2);
        this.sprite.runAction(a3);
        this.sprite.setLocalZOrder(1);
    } else {
        this.sprite.runAction(a1);

        if (isVertical)
            this.y = this.y + step;
        else
            this.x = this.x + step;
    }

    callback(Math.abs(step));
};